'''
Author: xubing
Date: 2021-08-31 16:39:22
LastEditors: xubing
LastEditTime: 2021-08-31 17:11:58
FilePath: /my_gists/多线程和多进程示例.py
'''

import time
import threading
import multiprocessing

max_process = 4
max_thread = max_process


def fun(n, n2):
    #cpu密集型
    for i in range(0, n):
        for j in range(0, (int)(n * n * n * n2)):
            t = i * j


def thread_main(n2):
    thread_list = []
    for i in range(0, max_thread):
        t = threading.Thread(target=fun, args=(50, n2))
        thread_list.append(t)

    start = time.time()
    print(' [+] much thread start')
    for i in thread_list:
        i.start()
    for i in thread_list:
        i.join()
    print(' [-] much thread use ', time.time() - start, 's')


def process_main(n2):
    p = multiprocessing.Pool(max_process)
    for i in range(0, max_process):
        p.apply_async(func=fun, args=(50, n2))
    start = time.time()
    print(' [+] much process start')
    p.close()  #关闭进程池
    p.join()  #等待所有子进程完毕
    print(' [-] much process use ', time.time() - start, 's')


def my_thred(n):
    print(n)


dates1 = range(1, 11)
dates2 = range(11, 21)
dates3 = range(21, 31)


def test():
    for i, j, k in zip(dates1, dates2, dates3):

        thread1 = threading.Thread(my_thred(i))
        thread2 = threading.Thread(my_thred(j))
        thread3 = threading.Thread(my_thred(k))
        thread1.start()
        thread2.start()
        thread3.start()


bb
if __name__ == '__main__':
    # print("[++]When n=50,n2=0.1:")
    # thread_main(0.1)
    # process_main(0.1)
    # print("[++]When n=50,n2=1:")
    # thread_main(1)
    # process_main(1)
    # print("[++]When n=50,n2=10:")
    # thread_main(10)
    # process_main(10)
    test()
